﻿using Axios.Engine;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Factories;
using FarseerPhysics.SamplesFramework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;

namespace axios_tennis
{
    class Ball : SimpleDrawableAxiosGameObject
    {
        public delegate void HitScoreWallHandler(int side);

        public event HitScoreWallHandler HitScoreWall;

        public Ball(Vector2 position)
        {
            this.Position = position;
            AllowAutomaticMouseJoint = true;
        }

        public override void LoadContent(AxiosGameScreen gameScreen)
        {
            base.LoadContent(gameScreen);
            this.ConstantVelocity = new Vector2(7f, 0f);
            this.Texture = gameScreen.ScreenManager.Game.Content.Load<Texture2D>("ball");
            this.Origin = new Vector2(this.Texture.Width / 2, this.Texture.Height / 2);
            this.BodyPart = BodyFactory.CreateCircle(gameScreen.World, ConvertUnits.ToSimUnits(this.Texture.Width / 2), 1f);

            this.BodyPart.Position = this.Position;
            this.BodyPart.BodyType = BodyType.Dynamic;
            this.BodyPart.Friction = 0f;
            this.BodyPart.Restitution = 1f;
            this.BodyPart.UserData = this;
            //BodyPart.FixedRotation = true;


            this.BodyPart.OnCollision += new OnCollisionEventHandler(BodyPart_OnCollision);
        }

        bool BodyPart_OnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
        {
            if (fixtureB.UserData is Paddle)
                ConstantVelocity += Vector2.One;
            if (fixtureB.UserData is Wall && HitScoreWall != null)
            {
                if (fixtureB.UserData.ToString() == "pwall")
                    HitScoreWall(0);
                else if (fixtureB.UserData.ToString() == "ewall")
                    HitScoreWall(1);

            }
            return true;
        }


        public override void OnMouseDown(AxiosGameScreen gameScreen, InputState input)
        {
            KickBall();
            base.OnMouseDown(gameScreen, input);
        }

        public void KickBall()
        {
            this.BodyPart.ApplyForce(new Vector2(-7f, 7f));
            this.BodyPart.ApplyAngularImpulse(25f);

            this.ApplyConstantVelocity = true;
        }

        public void ResetBall()
        {
            this.ApplyConstantVelocity = false;
            ConstantVelocity = new Vector2(7f, 0f);
            this.BodyPart.Position = Vector2.Zero;
            this.BodyPart.LinearVelocity = Vector2.Zero;
            BodyPart.AngularVelocity = 0f;
            this.BodyPart.Inertia = 0f;
        }
        public override void HandleInput(AxiosGameScreen gameScreen, InputState input, GameTime gameTime)
        {
            base.HandleInput(gameScreen, input, gameTime);

            PlayerIndex i;
            if (input.IsNewKeyPress(Keys.Space, PlayerIndex.One, out i) || input.IsNewVirtualButtonPress(Buttons.A) || input.IsNewButtonPress(Buttons.A, PlayerIndex.One, out i))
            {
                KickBall();
                ((TennisScreen)gameScreen).Play = true;
            }

            if (input.IsNewVirtualButtonPress(Buttons.B) || input.IsNewButtonPress(Buttons.B, PlayerIndex.One, out i))
            {
                ResetBall();
            }
        }

    }
}